package com.defysoft.waukids.attend;

import java.util.ArrayList;

import org.json.JSONObject;

import android.app.ProgressDialog;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Message;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.defysoft.waukids.BaseActivity;
import com.defysoft.waukids.Custom_Data_Type.Bc_Student_Att;
import com.defysoft.waukids.JsonParser;
import com.defysoft.waukids.R;
import com.defysoft.waukids.adapter.Adapter_AttendSelectMemeber_L;

public class Attend_SelectMember_A extends BaseActivity implements OnClickListener{

	private SharedPreferences school;
	private SharedPreferences member;
	private SharedPreferences school_class;
	Toolbar toolbar;
	int count = 0;
	ProgressDialog dialog;
	ArrayList<Bc_Student_Att> list;
	ListView listView;
	Adapter_AttendSelectMemeber_L adapter;
	final int HAVE_CLASS_MEMBER_T = 8;
	final int HAVE_CLASS_MEMBER_W = 9;
	final int NON_CLASS_MEMBER = 10;
	final int SEARCH_THREAD = 1;
	final int MAIN_THREAD = 2;
	String[] celebrities = { "수 정", "삭 제" };
	String studentcode, searchText;

	Spinner spinner;

	Button button;
	EditText editText;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_attend__select_member_);
		setStartData();
		setStartToolbar();
		dialog = ProgressDialog.show(Attend_SelectMember_A.this, "원생리스트 불러오는중",
				"잠시만 기다려주세요");
		

	}
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		startNetwork(MAIN_THREAD);
	}

	@Override
	public void setStartData() {
		// TODO Auto-generated method stub
		super.setStartData();
		studentcode=getIntent().getStringExtra(INTENT_EXTRA_ATTEND_PK_STU_CODE);
		listView = (ListView) findViewById(R.id.ATTENDSELECTMEMBER_LIST);
		member = getSharedPreferences("member", MODE_PRIVATE);
		school = getSharedPreferences("school", MODE_PRIVATE);
		school_class = getSharedPreferences("school_class", MODE_PRIVATE);
		toolbar = (Toolbar) findViewById(R.id.ATTENDSELECTMEMBER_TOOL);
		spinner = (Spinner) findViewById(R.id.ATTENDSELECTMEMBER_SPI);
		button =(Button)findViewById(R.id.ATTENDSELECTMEMBER_SEARCH_BT);
		button.setOnClickListener(this);
		editText = (EditText)findViewById(R.id.ATTENDSELECTMEMBER_SEARCH_ET);
		if (member.getString("Mem_Rank", "").equals("3")) {
			findViewById(R.id.ATTENDSELECTMEMBER_SEARCH_LL).setVisibility(View.GONE);
		}
	}

	private void setStartToolbar() {
		spinner.setAdapter(new ArrayAdapter<String>(this, 0) {
			@Override
			public int getCount() {
				// TODO Auto-generated method stub
				return celebrities.length;
			}

			@Override
			public View getDropDownView(int position, View convertView,
					ViewGroup parent) {
				final int pos = position;
				if (convertView == null) {
					convertView = getLayoutInflater().inflate(
							R.layout.overflowitem_row, null);
				}
				((TextView) convertView.findViewById(R.id.OVERFLOWITEM_TV))
						.setText(celebrities[position]);
				convertView.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View v) {
						Toast.makeText(Attend_SelectMember_A.this, "" + pos,
								Toast.LENGTH_SHORT).show();
					}
				});
				
				return convertView;
			}

			@Override
			public View getView(int position, View convertView, ViewGroup parent) {
				final int pos = position;
				if (convertView == null) {
					convertView = getLayoutInflater().inflate(
							R.layout.overflowitem_row, null);
				}
				((TextView) convertView.findViewById(R.id.OVERFLOWITEM_TV))
						.setBackgroundColor(0x00ffffff);
				return convertView;
			}
		});

		toolbar.setNavigationOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();
			}
		});
	}

	@Override
	public void startThread(int choice) {
		// TODO Auto-generated method stub
		super.startThread(choice);
		if (firstThread != null) {
			firstThread.interrupt();
		}
		final int cho = choice;
		firstThread = new Thread(new Runnable() {
			@Override
			public void run() {
				switch (cho) {
				case MAIN_THREAD:
					setMain();
					break;
				case SEARCH_THREAD:
					searchThread();
					break;
				}
			}
		});
		firstThread.start();

	}

	protected void searchThread() {
		JSONObject jsonObject = null;
		try {
			if (member.getString("Mem_Rank", "").equals("2")) { // 선생님.
				jsonObject = new JSONObject(publicHttpCallback(
						HTTP_SELECT_ATTEND_STUDENTLIST_SEARCH,
						"sch_code=" + school.getString("PK_Sch_Code", "")
								+ "&cla_code=" + school_class.getString("PK_Cla_Code", "")
								+ "&Stu_Name="+searchText));

				if (jsonObject.getBoolean("success")) {
					onNetworkCallback(HAVE_CLASS_MEMBER_T,
							jsonObject.getString("payload"));
					count = Integer.parseInt(jsonObject.getString("count"));
				} else {
					onNetworkCallback(NON_CLASS_MEMBER,
							jsonObject.getString("message"));
				}

			} else { // 원장선생님
				jsonObject = new JSONObject(publicHttpCallback(
						HTTP_SELECT_ATTEND_STUDENTLIST_SEARCH,
						"sch_code=" + school.getString("PK_Sch_Code", "")
				+"&Stu_Name="+searchText));
				if (jsonObject.getBoolean("success")) {
					onNetworkCallback(HAVE_CLASS_MEMBER_W,
							jsonObject.getString("payload"));
					count = Integer.parseInt(jsonObject.getString("count"));
				} else {
					onNetworkCallback(NON_CLASS_MEMBER,
							jsonObject.getString("message"));
				}
			}
		} catch (Exception e) {
			onNetworkCallback(CATCH_EXCEPTION, e.toString());
		}		
	}

	@Override
	protected void handlingMessage(Message _message) {
		// TODO Auto-generated method stub
		super.handlingMessage(_message);
		switch (_message.what) {
		case CATCH_EXCEPTION:
			endDIal();
			if (firstThread != null) {
				firstThread.interrupt();
			}
			if (dialog != null) {
				dialog.dismiss();
			}
			Toast.makeText(this, (String) _message.obj, Toast.LENGTH_LONG)
					.show();
			break;
		case NON_CLASS_MEMBER: // 원생이 없음
			endDIal();
			if (firstThread != null) {
				firstThread.interrupt();
			}
			if (dialog != null) {
				dialog.dismiss();
			}
			Toast.makeText(this, (String) _message.obj, Toast.LENGTH_LONG)
					.show();
			break;

		case HAVE_CLASS_MEMBER_T:
			endDIal();
			if (firstThread != null) {
				firstThread.interrupt();
			}
			if (dialog != null) {
				dialog.dismiss();
			}
			list = JsonParser.AttendSelectMember((String) _message.obj);
			adapter = new Adapter_AttendSelectMemeber_L(this, 0, list,
					getLayoutInflater());
			listView.setAdapter(adapter);
			break;

		case HAVE_CLASS_MEMBER_W:
			endDIal();
			if (firstThread != null) {
				firstThread.interrupt();
			}
			if (dialog != null) {
				dialog.dismiss();
			}
			list = JsonParser.AttendSelectMember((String) _message.obj);
			adapter = new Adapter_AttendSelectMemeber_L(this, 0, list,
					getLayoutInflater());
			listView.setAdapter(adapter);
			break;
		}
	}

	public void setMain() {
		JSONObject jsonObject = null;
		try {
			if (member.getString("Mem_Rank", "").equals("2")) { // 선생님.
				jsonObject = new JSONObject(publicHttpCallback(
						HTTP_SELECT_ATTEND_STUDENTLIST,
						"sch_code=" + school.getString("PK_Sch_Code", "")
								+ "&cla_code="
								+ school_class.getString("PK_Cla_Code", "")));
				if (jsonObject.getBoolean("success")) {
					onNetworkCallback(HAVE_CLASS_MEMBER_T,
							jsonObject.getString("payload"));
					count = Integer.parseInt(jsonObject.getString("count"));
				} else {
					onNetworkCallback(NON_CLASS_MEMBER,
							jsonObject.getString("message"));
				}

			} else { // 원장선생님
				jsonObject = new JSONObject(publicHttpCallback(
						HTTP_SELECT_ATTEND_STUDENTLIST,
						"sch_code=" + school.getString("PK_Sch_Code", "")));
				if (jsonObject.getBoolean("success")) {
					onNetworkCallback(HAVE_CLASS_MEMBER_W,
							jsonObject.getString("payload"));
					count = Integer.parseInt(jsonObject.getString("count"));
				} else {
					onNetworkCallback(NON_CLASS_MEMBER,
							jsonObject.getString("message"));
				}

			}
		} catch (Exception e) {
			onNetworkCallback(CATCH_EXCEPTION, e.toString());
		}
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.ATTENDSELECTMEMBER_SEARCH_BT:	//검색버튼
			if (!(editText.getText().toString().equals(""))) {
				searchText = editText.getText().toString();
				startNetwork(SEARCH_THREAD);
			}
			
			break;

		default:
			break;
		}
	}
}
